package avicit.bdp.dms.dss.rest;

import avicit.bdp.dms.dss.dto.DataResourceDTO;
import avicit.bdp.dms.dss.dto.DssSubscribeDTO;
import avicit.bdp.dms.dss.service.DssSubscribeService;
import avicit.bdp.dms.dss.vo.DssSubScribeVo;
import avicit.platform6.core.context.ThreadContextHelper;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 数据资源申请接口
 */
@RestController
@RequestMapping("/api/bdp/dms/dss/subscribe")
@Api(tags = "数据资源订阅接口")
public class DssSubscribeRest {

    @Autowired
    private DssSubscribeService subscribeService;

    @ApiOperation(value = "查询数据资源订阅列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "分类id", name = "typeId", dataType = "String"),
            @ApiImplicitParam(value = "查询关键字", name = "keyWord", dataType = "String"),
            @ApiImplicitParam(value = "审核状态", name = "auditStatus", dataType = "String"),
            @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "Int"),
            @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int")
    })
    @GetMapping("/list")
    public ResponseMsg<QueryRespBean<DssSubScribeVo>> getSubscribeList(
            @RequestParam(required = false) String typeId,
            @RequestParam(required = false) String keyWord,
            @RequestParam(required = false) Integer auditStatus,
            @RequestParam(defaultValue = "1", required = false) Integer pageNo,
            @RequestParam(defaultValue = "10", required = false) Integer pageSize) throws Exception {

        ResponseMsg<QueryRespBean<DssSubScribeVo>> responseMsg = new ResponseMsg<>();
        String userId = ThreadContextHelper.getUserId();
        QueryRespBean<DssSubScribeVo> queryRespBean = subscribeService.getSubscribeList(typeId, auditStatus, keyWord, userId, pageNo, pageSize);
        responseMsg.setResponseBody(queryRespBean);
        return responseMsg;
    }

    @ApiOperation(value = "审核")
    @PostMapping("/audit")
    public ResponseMsg<Boolean> audit(@RequestBody DssSubscribeDTO subscribeDTO) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        subscribeService.audit(subscribeDTO);
        responseMsg.setResponseBody(true);
        return responseMsg;
    }


    @ApiOperation(value = "查询数据资源审核列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "分类id", name = "typeId", dataType = "String"),
            @ApiImplicitParam(value = "查询关键字", name = "keyWord", dataType = "String"),
            @ApiImplicitParam(value = "审核状态", name = "auditStatus", dataType = "String"),
            @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "Int"),
            @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int")
    })
    @GetMapping("/audit/list")
    public ResponseMsg<QueryRespBean<DssSubScribeVo>> getAuditList(
            @RequestParam(required = false) String typeId,
            @RequestParam(required = false) String keyWord,
            @RequestParam(required = false) Integer auditStatus,
            @RequestParam(defaultValue = "1", required = false) Integer pageNo,
            @RequestParam(defaultValue = "10", required = false) Integer pageSize) throws Exception {

        ResponseMsg<QueryRespBean<DssSubScribeVo>> responseMsg = new ResponseMsg<>();
        QueryRespBean<DssSubScribeVo> queryRespBean = subscribeService.getSubscribeList(typeId, auditStatus, keyWord, null, pageNo, pageSize);
        responseMsg.setResponseBody(queryRespBean);
        return responseMsg;
    }

    @ApiOperation(value = "查看数据资源审核详情")
    @ApiImplicitParam(value = "id", name = "id", dataType = "String")
    @GetMapping("/getSubscribeById")
    public ResponseMsg<DataResourceDTO> getSubscribeById(@RequestParam String id) {
        ResponseMsg<DataResourceDTO> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(subscribeService.getSubscribeById(id));
        return responseMsg;
    }

    @ApiOperation(value = "查看审核意见")
    @ApiImplicitParam(value = "id", name = "id", dataType = "String")
    @GetMapping("/audit/view")
    public ResponseMsg<DssSubscribeDTO> viewAudit(@RequestParam String id) {
        ResponseMsg<DssSubscribeDTO> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(subscribeService.viewAudit(id));
        return responseMsg;
    }

    @ApiOperation(value = "添加订阅")
    @PostMapping("/addSubscribe")
    public ResponseMsg<Boolean> addSubscribe(@RequestBody DssSubscribeDTO subscribeDTO) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        subscribeService.addSubscribe(subscribeDTO);
        responseMsg.setResponseBody(true);
        return responseMsg;
    }

    @ApiOperation(value = "添加订阅")
    @ApiImplicitParam(value = "资源id集合", name = "dataResourceIds", dataType = "List", paramType = "body")
    @PostMapping("/add")
    public ResponseMsg<Boolean> add(@RequestBody List<String> dataResourceIds) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(subscribeService.add(dataResourceIds));
        return responseMsg;
    }

    @ApiOperation(value = "取消订阅")
    @ApiImplicitParam(value = "资源id集合", name = "ids", dataType = "List", paramType = "body")
    @DeleteMapping("/delete")
    public ResponseMsg<Boolean> delete(@RequestBody List<String> ids) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(subscribeService.delete(ids));
        return responseMsg;
    }
}
